package com.clps.algorithm.chapter08.二叉搜索树中两个结点之和;

import com.clps.algorithm.chapter07.二叉树每层的最大值.TreeNode;

import java.util.Stack;

public class BSTIteratorReversed {

    TreeNode cur;
    Stack<TreeNode> stack;

    public BSTIteratorReversed(TreeNode root) {
        this.cur = root;
        stack = new Stack<>();
    }

    public  boolean hasprev(){
        return cur!=null || !stack.isEmpty();
    }

    public int prev(){
        while (cur!=null){
            stack.push(cur);
            cur = cur.right;
        }
        cur = stack.pop();
        int val = cur.val;
        cur= cur.left;
        return  val;
    }

}
